package com.song;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.song.server.Invocation;
import com.song.server.RpcServer;

public class RPC {

	
	public RpcServer rpcServer;
	
	

	
public static <T> T getProxy(final Class<T> clazz,String host,int port) {
		
		final Client client = new Client(host,port);
		InvocationHandler handler = new InvocationHandler() {

			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {

				Invocation invo = new Invocation();
				invo.setInterfaces(clazz);
				invo.setMethod(new com.song.server.Method(method.getName(), method.getParameterTypes()));
				invo.setParams(args);
				client.invoke(invo);
				return invo.getResult();
			
			}
			

		};
		T t = (T) Proxy.newProxyInstance(RPC.class.getClassLoader(), new Class[] {clazz}, handler);
		return t;
	}

}
